package cn.lyjuan.android.demofive.ui.translate;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import cn.lyjuan.android.demofive.R;

/**
 * Created by chad on 25/10/2017.
 */

public class ThumbnailActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_compat_scaleup_start);
    }

    public void translate(View v)
    {
        Drawable drawable = this.getResources().getDrawable(R.drawable.img_01);

        Bitmap bitmap = drawableToBitmap(drawable);

        ActivityOptionsCompat compat = ActivityOptionsCompat.makeThumbnailScaleUpAnimation(v, bitmap, 0, 0);

        ActivityCompat.startActivity(this, new Intent(this, TargetActivity.class), compat.toBundle());
    }

    static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap
    {
        int width = drawable.getIntrinsicWidth();// 取drawable的长宽
        int height = drawable.getIntrinsicHeight();
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;// 取drawable的颜色格式
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);// 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);// 把drawable内容画到画布中
        return bitmap;
    }
}
